package com.egao.common.module.system.transfer;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.egao.common.core.web.JsonResult;
import com.egao.common.module.cms.entity.Article;
import com.egao.common.module.cms.service.ArticleService;
import com.egao.common.module.company.entity.Mutual;
import com.egao.common.module.company.entity.Preach;
import com.egao.common.module.company.entity.PreachCloud;
import com.egao.common.module.company.entity.Recruit;
import com.egao.common.module.company.mapper.MutualMapper;
import com.egao.common.module.company.mapper.PreachCloudMapper;
import com.egao.common.module.company.mapper.PreachMapper;
import com.egao.common.module.company.mapper.RecruitMapper;
import com.egao.common.module.company.service.MutualService;
import com.egao.common.module.company.service.PreachCloudService;
import com.egao.common.module.company.service.PreachService;
import com.egao.common.module.company.service.RecruitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 新闻类文件url替换
 * @author pyx
 */
@Api(tags = "文件地址替换")
@RestController
@Slf4j
public class FilesUrlReplaceController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private PreachService preachService;
    @Autowired
    private PreachMapper preachMapper;
    @Autowired
    private PreachCloudService preachCloudService;
    @Autowired
    private PreachCloudMapper preachCloudMapper;
    @Autowired
    private MutualService mutualService;
    @Autowired
    private MutualMapper mutualMapper;
    @Autowired
    private RecruitService recruitService;
    @Autowired
    private RecruitMapper recruitMapper;

    @ApiOperation("文章")
    public JsonResult articleUrlReplace(){
        List<Article> articleList = articleService.listAll();

        for (Article article:articleList) {
            if(!StrUtil.isBlank(article.getContent())){
                String content = article.getContent();

                //防止重复更新
                if(content.indexOf("http://192.168.1.5:9000") != -1){
                    continue;
                }

                //地址替换
                content = contentReplace(content);

                article.setContent(content);
            }

            if(!StrUtil.isBlank(article.getImgUrl())){
                String imgUrl = article.getImgUrl();

                //防止重复更新
                if(imgUrl.indexOf("http://192.168.1.5:9000") != -1){
                    continue;
                }

                imgUrl = imgUrl.replace("/upload","http://192.168.1.5:9000/zuel/old/files/upload");
                article.setImgUrl(imgUrl);
            }
        }

        articleService.updateBatchById(articleList);
        return JsonResult.ok("操作成功");
    }

    @ApiOperation("校内宣讲会")
    @GetMapping("/publicly/replace/preach/url")
    public JsonResult preachUrlReplace(){
        QueryWrapper<Preach> wrapper = new QueryWrapper<>();
        List<Preach> preachList = preachMapper.selectList(null);

        for (Preach preach:preachList) {
            if(!StrUtil.isBlank(preach.getContent())){
                String content = preach.getContent();

                //防止重复更新
                if(content.indexOf("http://192.168.1.5:9000") != -1){
                    continue;
                }

                //地址替换
                content = contentReplace(content);

                preach.setContent(content);
            }
        }

        preachService.updateBatchById(preachList);
        return JsonResult.ok("操作成功");
    }


    @ApiOperation("空中宣讲会")
    @GetMapping("/publicly/replace/preach/cloud/url")
    public JsonResult preachCloudUrlReplace(){
        List<PreachCloud> preachCloudList = preachCloudMapper.selectList(null);

        for (PreachCloud preachCloud:preachCloudList) {
            if(!StrUtil.isBlank(preachCloud.getContent())){
                String content = preachCloud.getContent();

                //防止重复更新
                if(content.indexOf("http://192.168.1.5:9000") != -1){
                    continue;
                }

                //地址替换
                content = contentReplace(content);

                preachCloud.setContent(content);
            }
        }

        preachCloudService.updateBatchById(preachCloudList);
        return JsonResult.ok("操作成功");
    }

    @ApiOperation("校内双选会")
    @GetMapping("/publicly/replace/mutual/url")
    public JsonResult MutualUrlReplace(){
        List<Mutual> mutualList = mutualMapper.selectList(null);

        for (Mutual mutual:mutualList) {
            if(!StrUtil.isBlank(mutual.getContent())){
                String content = mutual.getContent();

                //防止重复更新
                if(content.indexOf("http://192.168.1.5:9000") != -1){
                    continue;
                }

                //地址替换
                content = contentReplace(content);

                mutual.setContent(content);
            }
        }

        mutualService.updateBatchById(mutualList);
        return JsonResult.ok("操作成功");
    }

    @ApiOperation("招聘信息")
    @GetMapping("/publicly/replace/Recruit/url")
    public JsonResult RecruitUrlReplace(){
        List<Recruit> recruitList = recruitMapper.selectList(null);

        for (Recruit recruit:recruitList) {
            if(!StrUtil.isBlank(recruit.getContent())){
                String content = recruit.getContent();

                //防止重复更新
                if(content.indexOf("http://192.168.1.5:9000") != -1){
                    continue;
                }

                //地址替换
                content = contentReplace(content);

                recruit.setContent(content);
            }
        }

        recruitService.updateBatchById(recruitList);
        return JsonResult.ok("操作成功");
    }


    private String contentReplace(String content){
        //替换图片
        content = content.replaceAll("/ueditor/jsp/upload","http://192.168.1.5:9000/zuel/old/files/ueditor/jsp/upload");
        content = content.replaceAll("znufe","zuel");
        //替换ueditor小标签
        content = content.replaceAll("http://jyzx.zuel.edu.cn/static/umeditor/dialogs/attachment/fileTypeImages","http://192.168.1.5:9000/zuel/ue/images");
        //更新替换后错误的url
        content = content.replaceAll("http://jyzx.zuel.edu.cnhttp://192.168.1.5:9000","http://192.168.1.5:9000");

        return content;
    }
}
